package kr.hkit.androidcomplete01.ch07;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.view.View;

public class MirrorImage extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MyView(this));
	}

	class MyView extends View {

		private Paint paint;
		private Bitmap car, mirror;
		private int width, height;
		private Matrix matrix;
		private ComposeShader comp;

		public MyView(Context context) {
			super(context);
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			car = BitmapFactory.decodeResource(getContext().getResources(),
					R.drawable.korandoc);
			width = car.getWidth();
			height = car.getHeight();
			matrix = new Matrix();
			matrix.preScale(1, -1f);
			mirror = Bitmap.createBitmap(car, 0, 0, width, height, matrix,
					false);
			comp = new ComposeShader(new BitmapShader(mirror, TileMode.REPEAT,
					TileMode.REPEAT), new LinearGradient(0, height, 0, height
					+ height, Color.TRANSPARENT, Color.BLACK, TileMode.REPEAT),
					new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.BLACK);
			canvas.drawBitmap(car, 0,  0, null);
			paint.setShader(comp);
			canvas.drawRect(0,  height, width, height+height, paint);
		}
	}
}
